package com.ld.security.shop.blog.modules.cms.crawler;

import cn.hutool.core.lang.Console;
import com.ld.security.common.core.base.column.MapJson;
import com.ld.security.common.core.util.UuidUtil;
import com.ld.security.shop.blog.modules.cms.entity.CmsContent;
import com.ld.security.shop.blog.modules.cms.entity.CmsTag;
import com.ld.security.shop.blog.modules.cms.entity.CmsType;
import com.ld.security.shop.blog.modules.cms.service.ICmsContentService;
import com.ld.security.shop.blog.modules.cms.service.ICmsTagService;
import com.ld.security.shop.blog.modules.cms.service.ICmsTypeService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class CmsContentCrawler {

    private RestTemplate http = new RestTemplate();

    @Autowired
    private ICmsContentService iCmsContentService;

    @Autowired
    private ICmsTagService iCmsTagService;

    @Autowired
    ICmsTypeService iCmsTypeService;

    public CmsContent parseDetails(String url){

        String id = UuidUtil.getId(url);
        CmsContent  cmsContent= iCmsContentService.getById(id);

        if(cmsContent != null){
            return cmsContent;
        }

        String html = http.getForObject(url,String.class);
        Document document =  Jsoup.parse(html);
        Element contentCard =  document.select(".card-content").first();
        Element type = contentCard.select(".has-link-grey").first();
        Element title = contentCard.select(".title").first();
        Element content =  contentCard.select(".content").first();
        Element picture =  contentCard.select("img").first();

        String typeLabel = type.text();
        String typeId = UuidUtil.getId(typeLabel);
        if(iCmsTypeService.getById(typeId) == null){
            CmsType cmsType =  iCmsTypeService.newInstance();
            cmsType.setId(typeId);
            cmsType.setLabel(typeLabel);
            iCmsTypeService.save(cmsType);
        }
        MapJson mapJson =  new MapJson();
        mapJson.put("cmsType",typeLabel);
        cmsContent = iCmsContentService.newInstance();
        cmsContent.setId(id);
        if(picture != null){
            cmsContent.setPicture(picture.attr("src"));
        }

        cmsContent.setCmsType(typeId);
        cmsContent.setMapJson(mapJson);
        cmsContent.setAuthorId("付林林");
        cmsContent.setLabel(title.text());
        cmsContent.setContent(content.outerHtml());
        iCmsContentService.save(cmsContent);
        return cmsContent;
    }

    public int parsePage(String url){
        String html = http.getForObject(url,String.class);
        Document document =  Jsoup.parse(html);
        Elements cards =  document.select(".columns").get(0).select(".card");
        for (Element card : cards) {
            Element a = card.select(".title  a").first();
            if(a != null){
                parseDetails(newUrl(a.attr("href")));
            }else{
                Console.log(card);
            }

        }
        return cards.size();
    }

    public String newUrl(String url){
        String baseUrl ="https://fulinlin.com";
        if(url.startsWith("http")){
            return url;
        }else{
            return baseUrl+url;
        }
    }


}
